#include "stdafx.h"
#include "UDockable.h"
#include "UContainerWindow.h"
#include "UNonSpaceFillingContainerWindow.h"
#include <stdlib.h>
#include "UGLFont.h"
#include "UPopUpMenu.h"
#include "UWindowThemer.h"
#include "UControlContainer.h"
#include "Controls/UThemedButton.h"
#include "UStringUtils.h"
using std::max;

TDockable::~TDockable() {
}

bool TDockable::IsFocussed()
{
	return Parent->ActiveChild()==this && Parent->IsFocussed();
};

TNonSpaceFillingContainerWindow* TDockable::GetRoot()
{
	return Parent->GetRoot();
}

int TDockable::Width(){return Right-Left;}
int TDockable::Height(){return Bottom-Top;}

void TDockable::Close()
{
	Parent->RemoveChild(this);
}

void TDockable::AddToPopUpMenu(TPopUpMenu* Menu)
{
	Menu->AddEntry("Close"         , boost::bind(&TDockable::Close        ,this));
	Menu->AddEntry("Togg le Borders", boost::bind(&TDockable::ToggleBorders,this));
}

void TDockable::ToggleBorders() {
	bordersVisible = !bordersVisible;
}

TDockable::TDockable(TRectangle Rect, TContainerWindow* parent, TWindowThemer *wt, std::string name)
{
	Parent	= parent;
	Name	= name;
	Left	= Rect.Left;
	Right	= Rect.Right;
	Top		= Rect.Top;
	Bottom	= Rect.Bottom;
	bordersVisible = true;
	WindowCaptionRenderer = NULL;
	try {
		GLFont = TGLFontRef();
	} catch(std::exception& e) {
		cerr << STRFORMAT("Error while initialising font! (error: %s)\n",e.what() );
	}

	windowthemer = wt;

	themecontrols = new TControlContainer();
	wt->SetupControls(this);
	wt->UpdateControls(themecontrols, TRectangle(Left, Top, Right, Bottom));
	mouseX=-1;
	mouseY=-1;
}


bool TDockable::Dock(TDockable* WindowToDock, int X, int Y)
{return false;} //Child classes should handle this

void TDockable::MouseMove	(int x, int y, int mstate) 
{
	mouseX = x;
	mouseY = y;
	themecontrols->MouseMove(x-Left, y-Top, mstate);
}

void TDockable::KeyUp(SDL_KeyboardEvent* Key) 
{
}

void TDockable::KeyDown(SDL_KeyboardEvent* Key)
{
	if (Key->keysym.mod & KMOD_CTRL)
	{
		if (Key->keysym.sym == SDLK_b) ToggleBorders();
	}

	if (Key->keysym.mod & KMOD_ALT)
	{
		if (Key->keysym.sym == SDLK_F4) Close();
	}
	themecontrols->KeyDown(Key);
}

void TDockable::MouseDown(int X, int Y, uint8 button)
{
	if(!windowthemer) return;
// 	switch( windowthemer->mouseHit(this, X,Y) ) {
// 		case TWindowThemer::WT_RESIZE_UPPER_LEFT:
// 			break;
// 		default:
// 			break;
// 	}
// 	return;
	if ( button == SDL_BUTTON_LEFT )
	{	//Left mouse button pressed

		if (windowthemer->mouseHit(this, X, Y) == WT_MOUSEHIT_MOVEABLE)
		{	//On Top border

			TNonSpaceFillingContainerWindow* Root = GetRoot();
			if (Root->DraggingWindow == NULL)
				{
					Root->DragWindowLeft	= Left-X;
					Root->DragWindowTop		= Top -Y;
					Root->DragWindowWidth	= Width();
					Root->DragWindowHeight	= Height();
					Root->DraggingWindow	= this;
				}
		}

	}
	themecontrols->MouseDown(X-Left, Y-Top, button);
}

void TDockable::MouseLeave()
{
	themecontrols->MouseLeave();
}

void TDockable::MouseUp(int X, int Y, uint8 button)
{
	if ((Y<Top)&& ( button == SDL_BUTTON_RIGHT) )
 	{
 		TPopUpMenu* PopUpMenu = new TPopUpMenu;
 		AddToPopUpMenu(PopUpMenu);
 		PopUpMenu->PopUp(X, Y);
 	}
 	themecontrols->MouseUp(X-Left, Y-Top, button);
}

void TDockable::Draw( )
{
	if(windowthemer && bordersVisible) windowthemer->draw(this, IsFocussed(), mouseX, mouseY);
	glTranslatef(Left, Top, 0);
	themecontrols->Draw();
	glTranslatef(-Left, -Top, 0);
}

bool TDockable::Contains	(int x, int y)
{
	if(!windowthemer) return false;
	WT_MOUSEHIT_RESULT result = windowthemer->mouseHit(this, x,y);
	return bordersVisible ? result > WT_MOUSEHIT_OUTSIDE_BORDER : result == WT_MOUSEHIT_INSIDE_BORDER ;
}

bool TDockable::ResizeAtPos( int x, int y)
{
	if(!windowthemer || !bordersVisible) return false;
	WT_MOUSEHIT_RESULT result = windowthemer->mouseHit(this, x,y);
	return (result >= WT_MOUSEHIT_RESIZE_TOP_LEFT) && (result <= WT_MOUSEHIT_RESIZE_BOTTOM_RIGHT);
	return false;
}

void TDockable::SetPos(TRectangle Rect)
{
	Left	= Rect.Left;
	Right	= max(Rect.Right, Left+10);
	Top		= Rect.Top;
	Bottom	= max(Rect.Bottom, Top+10);

	if (!(WindowCaptionRenderer==NULL))
	{
		WindowCaptionRenderer->~TGLFontObj();
		WindowCaptionRenderer= NULL;
	};

	wt->UpdateControls(themecontrols, TRectangle(Left, Top, Right, Bottom));
}
